package com.his.core.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.Map;

public class JSONChange {
    public static ObjectMapper mapper = new ObjectMapper();
    static {
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE,
                true);
    }
    /**
     * 001.json转换成对象
     */
    public static <T> T jsonToObj(String jsonStr, Class<T> valueType) throws IOException {
        return mapper.readValue(jsonStr, valueType);
    }
    /**
     * 002.对象转换成json
     */
    public static String objToJson(Object obj) throws JsonProcessingException {
        return mapper.writeValueAsString(obj);
    }
    /**
     * 003.json转换成Map
     */
    public static Map jsonToMap(String jsonStr) throws IOException {
        return mapper.readValue(jsonStr, Map.class);
    }
    /**
     * 004.map转换成json
     */
    public static String mapToJson(Map<String, Object> map) throws IOException {
        return mapper.writeValueAsString(map);
    }

}